package com.ljy.jymail.search.Thread;

import java.util.concurrent.*;

public class ThreadTest {

    //新建线程池-线程数10
    //当前系统中池只有一两个，每一个异步任务直接提交给线程池，让线程自己去执行
    public static ExecutorService service = Executors.newFixedThreadPool(10);


    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main....start");


        CompletableFuture<String> image = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }


            System.out.println("查看商品的图片信息");
            return "hello.jpg";
        });

        CompletableFuture<String> attr = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("查看商品属性");
            return "黑色+256GB";
        });

        CompletableFuture<String> catalog = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("查看商品三级分类");
            return "手机";
        });


        //等待上述方法全部成功再调用下一个方法,组合多个方法
        CompletableFuture<Object> anyOf = CompletableFuture.anyOf(image, attr, catalog);
        anyOf.get();//等待其中一个的结果完成


        System.out.println("main...end....:"+anyOf.get());

    }


    public static class Thread01 extends Thread {
        @Override
        public void run() {
            System.out.println("当前线程"+Thread.currentThread().getId());
            int i = 10/2;
            System.out.println("线程"+Thread.currentThread().getId()+"运行结果:"+i);
        }
    }


    public static class Thread02 implements Runnable {
        @Override
        public void run() {
            System.out.println("当前线程"+Thread.currentThread().getId());
            int i = 10/2;
            System.out.println("线程"+Thread.currentThread().getId()+"运行结果:"+i);
        }
    }



    public static class Thread03 implements Callable<Integer> {

        @Override
        public Integer call() throws Exception {
            System.out.println("当前线程"+Thread.currentThread().getId());
            int i = 10/2;
            System.out.println("线程"+Thread.currentThread().getId()+"运行结果:"+i);
            return i;
        }
    }


}
